<!--
 * @Description: 角色管理页面
-->
<template>
  <BaseMainSlot class="patrol-point-manage">
    <template v-slot:btn-box>
      <ActionBar
        :page-no="130"
        :add-active="addActive"
        :del-active="delActive"
        :cancel-active="cancelActive"
        :func-auth-active="funcAuthActive"
        :func-app-auth-active="funcAppAuthActive"
        :has-func-auth="true"
        :has-app-auth="false"
        :has-save="false"
        :has-edit="false"
        :has-del="false"
        @funcAuth="funcAuthShow"
        @funcAppAuth="funcAppAuthShow"
        @add="add"
        @cancel="cancel"
        @edit="edit"
        @del="del"
        @save="save('formData')"
      />
    </template>
    <template v-slot:search-box>
      <div class="common-search-item">
        <div class="label">角色名称:</div>
        <div class="input">
          <el-input
            v-model="roleName"
            clearable
            @keyup.enter.native="search"
            @change="search"
            @clear="search"
          />
        </div>
      </div>
    </template>
    <template v-slot:table-box>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="calc(100% - 50px)"
        @row-click="handleEdit"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" :selectable="row => { return !row.indflag }" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="roleName" label="角色名称" min-width="150" />
        <el-table-column prop="description" label="描述" min-width="150" />
        <el-table-column prop="userNames" label="用户列表" min-width="300">
          <template v-if="scope.row.roleNo" slot-scope="scope">
            {{ scope.row.userNames ? scope.row.userNames.join('，') : '' }}
          </template>
        </el-table-column>
        <el-table-column label="权限列表" width="200">
          <template v-if="scope.row.roleNo" slot-scope="scope">
            <el-button class="text-btn-cursor" type="text" size="small" @click="funcAuthShow(scope.row, true)">查看权限</el-button>
          </template>
        </el-table-column>
        <el-table-column width="200" label="操作">
          <template v-if="scope.row.roleNo" slot-scope="scope">
            <el-button class="text-btn-cursor" type="text" size="small" @click="funcAuthShow(scope.row)">功能权限</el-button>
            <el-button v-if="judgePermission(130, 'edit')" class="text-btn-cursor" type="text" size="small" @click="edit(scope.row.roleNo)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="titleText + '角色'"
        :visible.sync="dialogFormVisible"
        width="40%"
        :before-close="cancel"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item label="角色名称:" prop="roleName" class="w100p">
            <el-input v-model="formData.roleName" clearable />
          </el-form-item>
          <el-form-item
            label="描述:"
            prop="description"
            class="w100p"
          >
            <el-input v-model="formData.description" clearable />
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
      <el-dialog
        title="分配功能权限"
        :visible.sync="dialogDataAuthFormVisible"
        width="40%"
        :before-close="cancel"
      >
        <DataAuthComponent
          v-if="dialogDataAuthFormVisible"
          ref="funcAppAuth"
          :role-no="formData.roleNo"
          :rolestu="rolestu"
          :auth-disabled="authDisabled"
          @roles="roles"
        />
        <div slot="footer" class="dialog-footer">
          <!-- <el-button class="zh-button-one" @click="cancel">
            取 消
          </el-button> -->
          <el-button
            class="zh-button-two"
            type="primary"
            @click="saveRole('formData')"
          >
            关 闭
          </el-button>
        </div>
      </el-dialog>
      <el-dialog
        title="分配App功能权限"
        :visible.sync="dialogDataAppAuthFormVisible"
        width="40%"
      >
        <DataAppAuthComponent
          v-if="dialogDataAppAuthFormVisible"
          ref="funcAuth"
          :role-no="formData.roleNo"
          :rolestu="rolestu"
          @roles="roles"
        />
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel">
            取 消
          </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="saveRole('formData')"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
      <!-- <el-form
        v-if="nowpage === 1"
        ref="formData"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-form-item label="角色名称:" prop="roleName" class="w100p">
          <el-input
            v-model="formData.roleName"
            :disabled="!canInput"
            clearable
          />
        </el-form-item>
        <el-form-item label="角色描述:" class="w100p">
          <el-input
            v-model="formData.remark"
            :disabled="!canInput"
            clearable
          />
        </el-form-item>
      </el-form> -->
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import DataAuthComponent from "../components/func-auth-component.vue";
import DataAppAuthComponent from "../components/App-auth-component.vue";
import roleManage from "@/api/system-settings/role-manage";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot";
import { mymixin } from "@/utils/mixins";
import { mapGetters } from 'vuex';

export default {
  name: "RoleManage",
  components: { ActionBar, Pagination, DataAuthComponent, DataAppAuthComponent, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      // 操作按钮的逻辑及展示数据
      addActive: true,
      editActive: false,
      delActive: false,
      saveActive: false,
      cancelActive: false,
      dataAuthActive: false,
      funcAuthActive: false,
      funcAppAuthActive: false,
      dialogDataAppAuthFormVisible: false,
      authDisabled: false,
      // table
      loading: true,
      // 当前页
      currentPage: 1,
      // 每页的数据条数
      pageSize: 10,
      // 总条数
      totalSize: 0,
      // 角色名称
      roleName: "",
      nowpage: 1,
      // 是否能输入
      canInput: false,
      tableData: [],
      rolestu: '',
      formData: {},
      rules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" }
        ]
      },
      // 选中的数据项数组 ID组成
      roleIdList: [],
      dialogTitle: "",
      dialogFormVisible: false,
      dialogDataAuthFormVisible: false
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  async created() {
    // 获取表格数据
    await this.getTableDataList();
  },
  mounted() {},
  methods: {
    async getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          roleName: this.roleName
        }
      };
      const { data: res } = await roleManage.getPageList(params);
      this.loading = false;
      let data = res;
      // 赋值总条数
      this.totalSize = data.totalElements;
      // 赋值数组
      this.tableData = data.content;
      this.addblankmet();
      this.dialogFormVisible = false
    },
    saveRole() {
      this.dialogDataAuthFormVisible = false

      this.funcAuthActive = false
      // this.rolestu = 'add'
    },
    roles() {
      this.rolestu = ''
      this.dialogDataAuthFormVisible = false
      this.dialogDataAppAuthFormVisible = false
    },
    // 填充表格空白行
    addblankmet() {
      let havline = this.tableData.length % this.pageSize;
      if (this.tableData.length < this.pageSize) {
        if (havline < this.pageSize) {
          const remain = this.pageSize - havline;
          for (var i = 0; i < remain; i++) {
            this.tableData.push({ indflag: i + this.tableData.length - 1 });
          }
        }
      }
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    handleEdit(row) {
      this.formData = JSON.parse(JSON.stringify(row));
      if (row.roleNo) {
        this.funcAuthActive = true;
        this.cancelActive = true;
        this.funcAppAuthActive = true;
      } else {
        this.resetActionBar()
      }
      // this.openDialog('add');
    },
    handleSelectionChange(list) {
      this.roleIdList = [];
      this.roleIdList = list.map((item) => item.roleNo);
      if (this.roleIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    // add() {
    //   // TODO 添加后 input可输入 add及cancel 1 其余 0
    //   this.canInput = true;
    //   this.addActive = false;
    //   this.editActive = false;
    //   this.delActive = false;
    //   this.saveActive = true;
    //   this.cancelActive = true;
    //   // this.nowpage = 1;
    //   this.dialogFormVisible = true
    //   // roleId置空
    //   // this.formData.roleNo = "";
    // },
    // 初始化操作栏状态
    resetActionBar() {
      this.funcAuthActive = false;
      this.dataAuthActive = false;
      this.assignUserActive = false;
      this.funcAuthActive = false;
      this.funcAppAuthActive = false;
      this.nowpage = 1;
      this.canInput = false;
      this.addActive = true;
      this.delActive = false;
      this.saveActive = false;
      this.cancelActive = false;
      this.editActive = false;
    },
    // 点击功能权限触发的方法
    funcAuthShow(data, disabled) {
      // this.openDialog('edit');
      // this.formData = data;
      this.authDisabled = !!disabled
      this.dialogDataAuthFormVisible = true;
    },
    funcAppAuthShow() {
      this.dialogDataAppAuthFormVisible = true
    },
    del(e) {
      this.$message.warning("角色不允许删除");
      // roleManage.allmetInfo({roleNoList: [e]}, "delList").then(res => {
      //   if (res.code === 1) {
      //     this.$message.success(res.msg);
      //     this.getTableDataList();
      //   }
      // });
    },
    cancel() {
      // this.closeDialog();
      this.formData = {};
      this.$nextTick(() => {
        this.$refs['formData'].resetFields();
      })
      this.funcAuthActive = false;
      this.dataAuthActive = false;
      this.dialogFormVisible = false;
      this.cancelActive = false;
      this.assignUserActive = false;
      this.funcAuthActive = false;
      this.funcAppAuthActive = false;
      this.addActive = true;
      this.dialogDataAuthFormVisible = false
      this.dialogDataAppAuthFormVisible = false
    },
    save(formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          if (this.formData.roleNo) {
            // 编辑接口
            roleManage.allmetInfo(this.formData, "edit").then((res) => {
              if (res.code === 1) {
                this.$message.success("修改成功！");
                this.formData = {};
                this.getTableDataList();
              }
            });
          } else {
            // 新增接口
            roleManage.allmetInfo(this.formData, "add").then((res) => {
              if (res.code === 1) {
                this.$message.success("新增成功！");
                this.formData = {};
                this.getTableDataList();
              } else {
                this.$message.warning(res.msg)
              }
            });
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
        if (this.formData.id && this.nowpage === 2) {
          this.$refs.funcAuth.setpermission();
          this.resetActionBar();
        } else if (this.nowpage === 3) {
          this.$refs.assignrole.checkchange();
        }
        this.resetActionBar();
      });
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = "";
    }
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";
.patrol-point-manage {
  width: 100%;
  height: 100%;
  ::v-deep .el-form-item__content {
    display: flex;
    align-items: center;
  }
  ::v-deep .el-form-item__label {
    min-width: 150px;
  }
  .dialog {
    ::v-deep .el-dialog__title {
      font-weight: bold;
    }
    .qrCode {
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  .common-layout {
    border: 1px solid $borderColor;
    height: calc(100% - #{$actionBarHeight + 10});
    padding: 20px;
    display: flex;
    .left-box {
      width: 50%;
      height: 100%;
      .search-box {
        display: flex;
        align-items: center;
        margin-bottom: 30px;
        .label-input {
          display: flex;
          align-items: center;
          .label {
            width: 80px;
            white-space: nowrap;
          }
        }
        .search {
          display: flex;
          align-items: center;
          img {
            height: 30px;
          }
        }
      }
      .table-box {
        height: calc(100% - 50px);
        position: relative;
        overflow-y: auto;
      }
    }
    .right-box {
      width: 49%;
      height: 100%;
      margin-left: 1%;
      border-left: 1px dashed $borderColor;
      padding-left: 20px;
      .content {
        height: 100%;
        .form {
          overflow-y: auto;
          height: 100%;
          .el-form {
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
          }
          .map {
            width: 100%;
            height: 300px;
          }
        }
      }
    }
  }
}
.el-dialog {
    .el-dialog__body {
      .el-table {
        max-height: 70vh !important;
      }
    }
  }
  // TODO:7/23
  ::v-deep .common-layout {
    height: 100%!important;
  }
</style>
